

#include <iostream>
#include <string>
#include <memory>

//抽象产品类
class Computer
{
public:
    Computer()
    {
    }

    void setBoard(const std::string &board)
    {
        _board = board;
    }
    void setDisplay(const std::string &display)
    {
        _display = display;
    }
    virtual void setOs() = 0;

    void show()
    {
        std::string param = "Computer Paramaters\n";
        param+= "\tBoard:  " + _board + "\n";
        param+= "\tDisplay:  " + _display + "\n";
        param+= "\tOS:  " + _os + "\n";

        std::cout << param << std::endl;
    }

protected:
    // 零件
    std::string _board;
    std::string _display;
    std::string _os;
};

//具体产品类
class Macbook : public Computer
{
public:
    void setOs() override
    {
        _os = "Mac OS";
    }
};

//设计抽象接口
class Builder
{
public:
    virtual void buildBoard(const std::string board) = 0;
    virtual void buildDisplay(const std::string board) = 0;
    virtual void buildOs() = 0;
    virtual std::shared_ptr<Computer> build() = 0;
};

//实现抽象接口
class MacBookBuilder : public Builder
{
public:
    MacBookBuilder() : _computer(new Macbook())
    {
    }
    void buildBoard(const std::string board) override
    {
        _computer->setBoard(board);
    }
    void buildDisplay(const std::string display) override
    {
        _computer->setDisplay(display);
    }
    void buildOs() override
    {
        _computer->setOs();
    }

    std::shared_ptr<Computer> build()
    {
        return _computer;
    }

private:
    //builder里组合了一个电脑
    std::shared_ptr<Computer> _computer;
};

//统一组装过程，提供给使用者组装
class Director
{
public:
    Director(Builder* builder) : _builder(builder)
    {

    }

    void construct(const std::string& board,const std::string& display)
    {
        _builder->buildBoard(board);
        _builder->buildDisplay(display);
        _builder->buildOs();


        //可以直接
        //return _builder->builder();
    }
private:
    //组合模式有一个builder
    std::shared_ptr<Builder> _builder;
};


int main()
{
    //创建 builder
    Builder* builder = new MacBookBuilder();

    //创建 director 并把上面创建的 builder给指挥者
    std::unique_ptr<Director> director(new Director(builder));

    //指挥者按照自己的方式来建造
    director->construct("intel","Nvidia");

    //获取对象
    std::shared_ptr<Computer> computer = builder->build();
    computer->show();

    
    return 0;
}